import data from './data.js'

function followUser(follower, followed, uid, token,way) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: data.httpAddr + "/user/follow/" + follower + "/" + followed+"/"+way,
            method: "GET",
            header: {
                uid,
                token
            },
            success: (res) => {
                if (res.data.code == 1) {
                    resolve({res:res, success: true, message: "关注成功！" })
                } else {
                    reject({res:res, success: false, message: res.data.msg })
                }
            },
            fail: (err) => {
                console.error("请求失败", err)
                reject({ success: false, message: "请求失败" })
            }
        })
    })
}
function cancelFollowUser(follower, followed, uid, token) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: data.httpAddr + "/user/cancelFollow/" + follower + "/" + followed,
            method: "GET",
            header: {
                uid,
                token
            },
            success: (res) => {
                if (res.data.code == 1) {
                    resolve({res:res, success: true, message: "取消关注成功！" })
                } else {
                    reject({res:res, success: false, message: res.data.msg })
                }
            },
            fail: (err) => {
                reject({ success: false, message: "请求失败" })
            }
        })
    })
}
function isFollow(follower, followed, uid, token) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: data.httpAddr + "/user/isFollow/" + follower + "/" + followed,
            method: "GET",
            header: {
                uid,
                token
            },
            success: (res) => {
                if (res.data.code == 1) {
                    resolve({ follow:res.data.data})
                } else {
                    reject({ success: false, message: res.data.msg })
                }
            },
            fail: (err) => {
                reject({ success: false, message: "请求失败" })
            }
        })
    })
}
function getMyLive( currentPage,pageSize,uid, token) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: data.httpAddr + "/live/getMyLive/" + uid + "/" + currentPage+"/"+pageSize,
            method: "GET",
            header: {
                uid,
                token
            },
            success: (res) => {
                if (res.data.code == 1) {
                    resolve({ data:res.data.data})
                } else {
                    reject({ success: false, message: res.data.msg })
                }
            },
            fail: (err) => {
                reject({ success: false, message: "请求失败" })
            }
        })
    })
}
function deleteMyLive( postId,uid, token) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: data.httpAddr + "/live/deleteLive",
            method: "DELETE",
            header: {
                uid,
                token
            },
			data:{
				"data":{
				    "uid":uid,
				    "postId":postId
				},
				"msgType":"deleteLive"
			},
            success: (res) => {
                if (res.data.code == 1) {
                    resolve({ data:res.data.msg})
                } else {
                    reject({ success: false, message: res.data.msg })
                }
            },
            fail: (err) => {
                reject({ success: false, message: "请求失败" })
            }
        })
    })
}
module.exports = { followUser,isFollow,cancelFollowUser,getMyLive,deleteMyLive}
